import Cookies from 'js-cookie'

const state = {
  sidebar: {
    opened: Cookies.get('sidebarStatus') ? !!+Cookies.get('sidebarStatus') : true,
    withoutAnimation: false,
    showeditPassword: false,
    showeditInfo: false
  },
  device: 'desktop',
  acadyearterm: null,
  networkMonitor: {
    online: false, offline: false, popUp: false, content: '网络正常'
  },
  schoolConfig: null,
  forbidden: {},
  pagesName: '',
  routePath: ''
}

const mutations = {
  TOGGLE_SIDEBAR: state => {
    state.sidebar.opened = !state.sidebar.opened
    state.sidebar.withoutAnimation = false
    if (state.sidebar.opened) {
      Cookies.set('sidebarStatus', 1)
    } else {
      Cookies.set('sidebarStatus', 0)
    }
  },
  CLOSE_SIDEBAR: (state, withoutAnimation) => {
    Cookies.set('sidebarStatus', 0)
    state.sidebar.opened = false
    state.sidebar.withoutAnimation = withoutAnimation
  },
  TOGGLE_DEVICE: (state, device) => {
    state.device = device
  },
  TOGGLE_ACADYEARTERM: (state, acadyearterm) => {
    state.acadyearterm = acadyearterm
  },
  TOGGLE_NETWORK: (state, networkMonitor) => {
    state.networkMonitor = networkMonitor
  },
  TOGGLE_SCHOOL: (state, schoolConfig) => {
    state.schoolConfig = schoolConfig
  },
  TOGGLE_FORBIDDEN: (state, forbidden) => {
    state.forbidden = forbidden
  },
  TOGGLE_PAGESNAME: (state, pagesName) => {
    state.pagesName = pagesName
  },
  TOGGLE_ROUTEPATH: (state, routePath) => {
    state.routePath = routePath
  }
}

const actions = {
  toggleSideBar({ commit }) {
    commit('TOGGLE_SIDEBAR')
  },
  closeSideBar({ commit }, { withoutAnimation }) {
    commit('CLOSE_SIDEBAR', withoutAnimation)
  },
  toggleDevice({ commit }, device) {
    commit('TOGGLE_DEVICE', device)
  },
  // 学年学期
  toggleAcadyearterm({ commit }, acadyearterm) {
    localStorage.setItem('admin_acadyearterm', JSON.stringify(acadyearterm))
    commit('TOGGLE_ACADYEARTERM', acadyearterm)
  },
  // 网络检查
  toggleNetworkMonitor({ commit }, networkMonitor) {
    commit('TOGGLE_NETWORK', networkMonitor)
  },
  // 学校信息
  toggleSchoolConfig({ commit }, schoolConfig) {
    commit('TOGGLE_SCHOOL', schoolConfig)
  },
  // 自定义键盘事件
  toggleForbidden({ commit }, forbidden) {
    commit('TOGGLE_FORBIDDEN', forbidden)
  },
  // 性能监控
  togglePagesName({ commit }, pagesName) {
    commit('TOGGLE_PAGESNAME', pagesName)
  },
  // 页面路由
  toggleRoutePath({ commit }, routePath) {
    commit('TOGGLE_ROUTEPATH', routePath)
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
